using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

using Camalon.Drawing;
using Camalon.Drawing.Design;


namespace Camalon.Drawing.Design
{
	/// <summary>
	/// Summary description for TextureBrushDesigner.
	/// </summary>
	public class TextureFillDesigner:ComponentBaseDesigner
	{
		public TextureFillDesigner()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public TextureFill TextureFill
		{
			get	
			{
				return this.Component as TextureFill;
			}
		}

		DesignerVerb			_verbSelectShape	= null;
		DesignerVerbCollection	_verbs				= null;


		public override DesignerVerbCollection Verbs
		{
			get
			{
//				return base.Verbs;

				if( _verbs == null ) 
				{
					_verbs = new DesignerVerbCollection();
					_verbSelectShape			= new DesignerVerb("Select Shape", new EventHandler(OnSelectShape));


					_verbs.Add( _verbSelectShape );
				}


				return _verbs;

			}
		}

		static PropertyDescriptor	_propImage		= TypeDescriptor.GetProperties(typeof( TextureFill ))["Image"];

		private void OnSelectShape(object sender, EventArgs e)
		{

			TextureFill texture = this.TextureFill;
			if( texture == null) return;

			IReferenceService rs = base.ReferenceService;
			object[] refs = rs.GetReferences( typeof(Shape));
			
			if( refs == null || refs.Length == 0) return;
			
			Shape[] shapes = new Shape[refs.Length];
			refs.CopyTo( shapes , 0);


			IDesignerHost host = this.DesignerHost;

			ShapeSelectorDialog shapeSelector = new ShapeSelectorDialog( shapes );
			if( shapeSelector.ShowDialog() == DialogResult.OK)
			{
				Bitmap bmp = null;
 
				Shape shape = shapeSelector.SelectedShape;
				if( shape != null) bmp = shape.GetBitmap();
			
				//Reset Transformations
				using(DesignerTransaction tx = host.CreateTransaction("Setting image shape"))
				{
					_propImage.SetValue( texture, bmp);
					tx.Commit();
				}

			}


		}

	}
}
